package com.systop.commer;

import com.alipay.api.internal.util.codec.Base64;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
/**
 *
 *
 * @author chensh
 * @email 1508877606@qq.com
 * @date 2020-07-27 15:43:42
 */
public class Io {

    //base64 解码
    public static String decode(byte[] bytes) {
        return new String(Base64.decodeBase64(bytes));
    }

    //base64 编码
    public static String encode(byte[] bytes) {
        return new String(Base64.encodeBase64(bytes));
    }


    public static String img(MultipartFile msg,HttpServletRequest request) throws IOException {
            String originalFilename = msg.getOriginalFilename();
            //查找项目中是否有upload路径
        //String path = request.getSession().getServletContext().getRealPath("/uploads");
        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/uploads/";
        //本地路径
      //  String path = "D:\\upload/upload";
            //新建file文件
            File filepath = new File(path);
            if (!filepath.exists()) {
                filepath.mkdirs();
            }
            //生成新的名字
            String newFileName = UUID.randomUUID() + "_" + originalFilename;
            //使用MultipartFile类transferTo方法上传参数是个文件filepath+newFileName==/upload/xxx_1.txt
            File targetFile1 = new File(filepath, newFileName);
            msg.transferTo(targetFile1);
            return newFileName;
    }
}
